package com.example.tool.erp.demo;

import com.example.tool.erp.sdk.Client;
import com.example.tool.erp.sdk.Pager;
import com.example.tool.erp.sdk.WdtErpException;
import com.example.tool.erp.sdk.api.wms.stockother.in.StockOtherInAPI;
import com.example.tool.erp.sdk.api.wms.stockother.in.dto.StockOtherInPushRequest;
import com.example.tool.erp.sdk.api.wms.stockother.in.dto.StockOtherInPushResponse;
import com.example.tool.erp.sdk.api.wms.stockother.in.dto.StockOtherInQueryRequest;
import com.example.tool.erp.sdk.api.wms.stockother.in.dto.StockOtherInQueryResponse;
import com.example.tool.erp.sdk.impl.ApiFactory;
import com.example.tool.erp.sdk.impl.DefaultClient;

import java.util.ArrayList;
import java.util.List;

public class StockOtherIn
{
//	public static void main(String[] args) throws WdtErpException
//	{
//		Client client = DefaultClient.get("wdterp30", "http://172.172.1.62:30000/", "xyx_api",
//				"e8866c1681dacc9488a19c89991791b8:8f5800dab84b7a8d614e92cba739032c");
//		StockOtherInAPI stockOtherInAPI = ApiFactory.get(client, StockOtherInAPI.class);
//
////		queryWithDetail(stockOtherInAPI);
//		testPush(stockOtherInAPI);
//	}

	private static void queryWithDetail(StockOtherInAPI api) throws WdtErpException
	{
		int pageSize = 5;
		Pager pager = new Pager(pageSize, 0, true);
		StockOtherInQueryRequest request = new StockOtherInQueryRequest();
		request.setStartTime("2022-09-01");
		request.setEndTime("2022-09-30");
		StockOtherInQueryResponse response = api.queryWithDetail(request, pager);

		Integer total = response.getTotal();
		if (null == total || pageSize >= total)
		{
			System.out.println("处理数据");
			return ;
		}

		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
		pager.setCalcTotal(false); //后续翻页不需要计算总条数, 可以大大减少请求时间
		for (int i = totalPage; i >= 0; i--)// 从后向前翻页
		{
			pager.setPageNo(i);
			System.out.print("pager: page_size:" + pageSize + "  page_no: " + i + "  ");
			response = api.queryWithDetail(request, pager);
			System.out.println("处理数据");
		}
	}

	private static void testPush(StockOtherInAPI api) throws WdtErpException
	{
		StockOtherInPushRequest.Order order = new StockOtherInPushRequest.Order();
		order.setWarehouseNo("xyx");

		List<StockOtherInPushRequest.OrderDetail> detailList = new ArrayList<>();
		StockOtherInPushRequest.OrderDetail detail = new StockOtherInPushRequest.OrderDetail();
		detailList.add(detail);

		detail.setSpecNo("gy01");
		detail.setNum(2);

		StockOtherInPushResponse response = api.push(order, detailList);

		System.out.println(response.getStatus());
		System.out.println(response.getMessage());
	}
}
